﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pheonix.CodeLibrary.Web;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.DAL;
using System.Text;
using System.IO;

public partial class page_private_admin_productcategorydetail : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = SiteName + " - Product Category Details [ Admin ]";

        if (!IsPostBack)
        {
            try
            {
                BindControls();

                if (Request["id"] != null)
                {
                    int id = 0;
                    if (int.TryParse(Request["id"], out id))
                        LoadEditData(id);
                }
            }
            catch
            {
                uccResponseMessage.SetFailMessage("There was a problem in loading the page");
            }
        }
        if (Request["id"] == null)
            lblTitle.Text = "Add a new category";
        else
            lblTitle.Text = "Update category";
    }

    private void BindControls()
    {
        CustomCollection<Category> categories = Category.GetAllCategory();
        ddlParentCategory.Items.Clear();
        ddlParentCategory.DataSource = categories;
        ddlParentCategory.DataTextField = "CategoryName";
        ddlParentCategory.DataValueField = "Id";
        ddlParentCategory.Items.Insert(0, new ListItem("Select", "-1"));
        ddlParentCategory.DataBind();

        if (categories.Count == 0)
            rqvCategoryId.Enabled = false;
        else
            rqvCategoryId.Enabled = true;

        if (!IsPostBack)
            imgCategoryImage.ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/imageNA.gif";
    }

    private void LoadEditData(int editDataId)
    {
        Category editCategory = null;
        if (editDataId != 0)
            editCategory = Category.GetCategoryById(editDataId);
        if (editCategory != null)
        {
            tbxCategoryName.Text = editCategory.CategoryName;
            ddlParentCategory.SelectedValue = editCategory.ParentCategoryId.ToString();
            imgCategoryImage.ImageUrl = editCategory.CategoryImage;
        }
        BindControls();
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            int categoryId = 0;
            Category editCategory = null;
            string categoryOldName = string.Empty;
            if(Request["id"] != null )
            {
                if (int.TryParse(Request["id"], out categoryId))
                {
                    editCategory = Category.GetCategoryById(categoryId);
                    if(editCategory!=null)
                        categoryOldName = editCategory.CategoryName;
                }
            }

            string categoryName = Utility.FilterInputText(tbxCategoryName.Text);
            if (categoryName == categoryOldName || CustomCommand.IsCategoryNameFree(categoryName))
            {        
                string imagePath = null;
                if (!string.IsNullOrEmpty(fluCategoryImage.FileName))
                {
                    string tempImagePath = (string)(new AppSettingsReader().GetValue("TempPath", typeof(string)));
                    string mimeType = fluCategoryImage.PostedFile.ContentType;

                    if (mimeType == "image/jpeg" || mimeType == "image/gif" || mimeType == "image/png")
                    {
                        string tempFileName = tempImagePath + Guid.NewGuid() + 
                            Path.GetExtension(fluCategoryImage.FileName);
                        fluCategoryImage.SaveAs(MapPath(tempFileName));

                        imagePath = CategoryManager.UploadCategoryImage(tempFileName, categoryName);
                    }
                    else
                        uccResponseMessage.SetFailMessage("Only JPG, GIF, PNG image allowed");
                }
                
                int parentCategoryId = -1;
                if (ddlParentCategory.SelectedValue != null)
                    int.TryParse(ddlParentCategory.SelectedValue, out parentCategoryId);

                if (editCategory != null)
                {   
                    editCategory.CategoryName = categoryName;
                    editCategory.ParentCategoryId = parentCategoryId;
                    if(!string.IsNullOrEmpty(imagePath))
                        editCategory.CategoryImage = imagePath;
                    editCategory.LastUpdate = DateTime.Now;

                    if (editCategory.Save())
                    {
                        uccResponseMessage.SetSuccessMessage("Category successfuly updated");
                        LoadEditData(categoryId);
                    }
                    else
                        uccResponseMessage.SetFailMessage("Could not update category");
                }
                else
                {
                    int newCategoryId = Category.CreateNewCategory(categoryName,parentCategoryId,0,
                        DateTime.Now, DateTime.Now, 0, imagePath);

                    if (newCategoryId > 0)
                    {
                        uccResponseMessage.SetSuccessMessage("Category successfuly created");
                        LoadEditData(newCategoryId);
                    }
                    else
                        uccResponseMessage.SetFailMessage("Could not create category");
                }
            }
            else
                uccResponseMessage.SetFailMessage("There is already a category with this name");
        }
        else
            uccResponseMessage.SetFailMessage("Could not save because of invalid data");
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("productcategorylist.aspx");
    }
}
